import java.util.Stack;

public class Sort {
    //直接插入排序
    public static void insetSort(int[] array){
        for(int i=1;i<array.length;i++){
            int tmp=array[i];
            int j=i-1;
            for(;j>=0;j--){
                if(array[j]>tmp){
                    array[j+1]=array[j];
                }else{
                    array[j+1]=tmp;
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }
    //希尔排序
    public static void shellSort(int[] array){
        int gap=array.length;
        while(gap>1){
            gap/=2;
            Shell(array,gap);
        }
    }
    public static void Shell(int[] array,int gap){
        for(int i=gap;i<array.length;i++){
            int tmp=array[i];
            int j=i-gap;
            for(;j>=0;j-=gap){
                if(array[j]>tmp){
                    array[j+gap]=array[j];
                }else{
                    array[j+gap]=tmp;
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }
    //选择排序
    public static void selectSort(int[] array){
        for(int i=0;i<array.length;i++){
            int min=i;
            for(int j=i+1;j<array.length;j++){
                if(array[min]>array[j]){
                    min=j;
                }
            }
            swap(array,min,i);
        }
    }
    public static void swap(int[] array,int i,int j){
        int mid=array[i];
        array[i]=array[j];
        array[j]=mid;
    }
    public static void selectSort2(int[] array){
        int left=0;
        int right=array.length-1;
        while(left<right){
            int max=left;
            int min=left;
            for(int i=left+1;i<=right;i++){
                if(array[max]<array[i]){
                    max=i;
                }
                if(array[min]>array[i]){
                    min=i;
                }
            }
            swap(array,min,left);
            if(max==left){
                max=min;
            }
            swap(array,max,right);
            left++;
            right--;
        }
    }
    //堆排序
    public static void heapSort(int[] array){
        createHeap(array);
        for(int i=array.length-1;i>0;i--){
            swap(array,0,i);
            siftDown(array,0,i-1);
        }
    }
    public static void createHeap(int[] array){
        for(int i=(array.length-2)/2;i>=0;i--){
            siftDown(array,i,array.length);
        }
    }
    public static void siftDown(int[] array,int parent,int end){
        int child=parent*2+1;
        while(child<end){
            if(child+1<end&&array[child]<array[child+1]){
                child++;
            }
            if(array[parent]<array[child]){
                swap(array,parent,child);
                parent=child;
                child=parent*2+1;
            }else{
                break;
            }
        }
    }
    //冒泡排序
    public static void bubbleSort(int[] array){
        for(int i=0;i<array.length-1;i++){
            int flag=0;
            for(int j=0;j<array.length-1-i;j++){
                if(array[j+1]<array[j]){
                    swap(array,j,j+1);
                    flag=1;
                }
            }
            if(flag==0){
                break;
            }
        }
    }
    //快速排序
    public static void quickSort(int[] array){
        quickNor(array,0,array.length-1);
    }
    private static void quick(int[] array, int left, int right) {
        if(left>=right){
            return;
        }
        if(right-left<10){
            insert(array,left,right);
        }
        int middle=getMiddleNum(array,left,right);
        swap(array,left,middle);
        //int mid=partitionHoare(array,left,right);
        //int mid = partition(array,left,right);
        int mid = partition2(array,left,right);
        quick(array,left,mid-1);
        quick(array,mid+1,right);
    }
    public static void quickNor(int[] array,int left,int right){
        Stack<Integer> stack =new Stack<>();
        int mid = partition(array,left,right);
        if(left<mid-1) {
            stack.push(left);
            stack.push(mid-1);
        }
        if(right>mid+1){
            stack.push(mid+1);
            stack.push(right);
        }
        while(!stack.empty()){
            right = stack.pop();
            left = stack.pop();
            mid = partition(array,left,right);
            if(left<mid-1) {
                stack.push(left);
                stack.push(mid-1);
            }
            if(right>mid+1){
                stack.push(mid+1);
                stack.push(right);
            }
        }
    }
    private static int getMiddleNum(int[] array,int left,int right){
        int mid=(left+right)/2;
        if(array[left]<array[right]){
            if(array[mid]>array[right]){
                return right;
            }else if(array[mid]<array[left]){
                return left;
            }else{
                return mid;
            }
        }else{
            if(array[mid]>array[left]){
                return left;
            }else if(array[mid]<array[right]){
                return right;
            }else{
                return mid;
            }
        }
    }
    private static void insert(int[] array,int left,int right){
        for(int i=left+1;i<=right;i++){
            int tmp=array[i];
            int j=i-1;
            for(;j>=0;j--){
                if(array[j]>tmp){
                    array[j+1]=array[j];
                }else{
                    array[j+1]=tmp;
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }


    private static int partitionHoare(int[] array,int left,int right){
        int start=left;
        int end=right;
        while(end>start){
            while(end>start&&array[end]>=array[left]){
                end--;
            }
            while(start<end&&array[start]<=array[left]){
                start++;
            }
            swap(array,start,end);
        }
        swap(array,left,start);
        return start;
    }
    private static int partition(int[] array,int left,int right){
        int tmp = array[left];
        while(left<right){
            while(left<right&&array[right]>=tmp){
                right--;
            }
            array[left]=array[right];
            while(left<right&&array[left]<=tmp){
                left++;
            }
            array[right]=array[left];
        }
        array[left]=tmp;
        return left;
    }
    private static int partition2(int[] array,int left,int right){
        int cur=left+1;
        int prev=left;
        while(cur<=right){
            if(array[cur]<array[left]&&array[++prev]!=array[cur]){
                swap(array,prev,cur);
            }
            cur++;
        }
        swap(array,left,prev);
        return prev;
    }
    public static void mergeSort(int[] array) {
        mergeSortTmp(array,0,array.length-1);
    }
    private static void mergeSortTmp(int[] array,int left,int right){
        if(left>=right){
            return;
        }
        int mid = (left+right)/2;
        mergeSortTmp(array,left,mid);
        mergeSortTmp(array,mid+1,right);
        merge(array,left,mid,right);
    }
    private static void merge(int[] array,int left,int mid,int right){
        int[] k = new int[right-left+1];
        int i=0;
        int s1 = left;
        int s2 = mid+1;
        while(s1<=mid&&s2<=right){
            if(array[s1]<array[s2]){
                k[i] = array[s1];
                i++;
                s1++;
            }else{
                k[i] = array[s2];
                i++;
                s2++;
            }
        }
        while(s2<=right){
            k[i]=array[s2];
            i++;
            s2++;
        }
        while(s1<=mid){
            k[i]=array[s1];
            i++;
            s1++;
        }
        for(int j = 0;j<k.length;j++){
            array[j+left] = k[j];
        }
    }
    public static void mergeSortNor(int[] array){
        int gap = 1;
        while(gap<array.length){
            for(int i = 0;i<array.length;i=i+2*gap){
                int left = i;
                int mid = i+gap-1;
                if(mid >= array.length){
                    mid = array.length-1;
                }
                int right = i+2*gap-1;
                if(right >= array.length){
                    right = array.length-1;
                }
                merge(array,left,mid,right);
                gap *= 2;
            }
        }
    }
    //计数排序
    public static void countSort(int[] array){
        int minVal = array[0];
        int maxVal = array[0];
        int k = 0;
        for(int i = 1;i < array.length;i++){
            if(array[i]<minVal){
                minVal = array[i];
            }
            if(array[i] >maxVal){
                maxVal = array[i];
            }
        }
        int[] tmp = new int[maxVal-minVal+1];
        for(int i = 0;i < array.length;i++){
            tmp[array[i]-minVal]++;
        }
        for(int i = 0;i < tmp.length;i++){
            while(tmp[i]!=0){
                array[k++]=i+minVal;
                tmp[i]--;
            }
        }
    }
}
